# 小顶堆：任意节点的值小于其子节点的值
# 大顶堆：任意节点的值大于其子节点的值
# 堆是完全二叉树的一个特例，具备一下特性：
#   最底层节点靠左填充，其他层的节点都被填满
#   二叉树的根节点称为堆顶，将底层最靠右的节点称为堆底
#   对于大顶堆，堆顶元素的值是最大的；反之对于小堆顶，堆顶元素的值是最小的
import heapq

if __name__ == '__main__':
    min_heap, flag = [], 1
    max_heap, flag = [], -1
    heapq.heappush(max_heap, flag * 1)
    heapq.heappush(max_heap, flag * 3)
    heapq.heappush(max_heap, flag * 2)
    heapq.heappush(max_heap, flag * 5)
    heapq.heappush(max_heap, flag * 4)
    print(max_heap)
    peek = flag * max_heap[0]
    print(peek)

    # 出堆元素会形成一个从大到小的序列
    val = flag * heapq.heappop(max_heap)
    print(val)
    val = flag * heapq.heappop(max_heap)
    print(val)
    val = flag * heapq.heappop(max_heap)
    print(val)
    val = flag * heapq.heappop(max_heap)
    print(val)
    val = flag * heapq.heappop(max_heap)
    print(val)

    # 获取堆大小
    size = len(max_heap)

    # 判断堆是否为空
    is_empty = not max_heap

    # 输入列表并建堆
    min_heap = [1, 3, 2, 5, 4]
    heapq.heapify(min_heap)